<template>
  <div class="wrap">
    <CateList
      dictionaryKey="opinionKeyManager"
      :search="handleSearch"
      ref="cateList"
      v-model:form="searchParams"
    ></CateList>
    <div class="wrap_right">
      <BaseForm ref="searchForm" type="search" :items="formItems" v-model:form="searchParams" @search="handleSearch">
        <template #suffix-button>
          <el-button type="primary" @click="handleEdit({})">
            <el-icon><Plus /></el-icon>
            添加
          </el-button>
        </template>
      </BaseForm>

      <BaseTable
        ref="baseTable"
        :getTableDataFn="contentManagementService.libOpinionKeyPageList"
        :searchParams="searchParams"
        :serial="true"
        :select="true"
        :handleDisable="handleDisable"
      >
        <template #select>
          <el-table-column type="selection" width="55" />
        </template>
        <el-table-column prop="opinionKey" label="舆情词" minWidth="170" />
        <el-table-column prop="level" label="类型" minWidth="170">
          <template #default="scope">
            <el-text v-if="scope.row.level === 1">重要舆情词</el-text>
            <el-text v-if="scope.row.level === 2">一般舆情词</el-text>
          </template>
        </el-table-column>
        <el-table-column prop="typeName" label="所属分类" minWidth="170" />
        <el-table-column prop="updateTime" label="最后修改时间" minWidth="170">
          <template #header>
            <span>最后修改时间</span>
            <SortTable :sortTablekey="sortTablekey" @click="sortTablekey = 1" :handleSearch="handleSearch"></SortTable>
          </template>
        </el-table-column>
        <el-table-column prop="isDisable" label="状态" width="60">
          <template #default="scope">
            <isDisableStatus :status="scope.row.isDisable"></isDisableStatus>
          </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right" width="130">
          <template #default="scope">
            <el-link :underline="false" link type="primary" size="small" @click.prevent="handleEdit(scope.row)"
              >编辑</el-link
            >
            <el-link :underline="false" link type="primary" size="small" @click.prevent="handleDisable(scope.row)">{{
              scope.row.isDisable == 1 ? '启用' : '停用'
            }}</el-link>
            <el-link :underline="false" link type="danger" size="small" @click.prevent="handleDel(scope.row)"
              >删除</el-link
            >
          </template>
        </el-table-column>
      </BaseTable>
    </div>
    <EditForm
      v-if="visibleEditForm"
      :searchParams="searchParams"
      v-model:dialogVisible="visibleEditForm"
      :item="item"
      :handleSearch="handleEditForm"
    ></EditForm>
  </div>
</template>

<script setup lang="ts">
import EditForm from './EditForm.vue';
import { contentManagementService } from '@/api/contentManagement';
import openMsgBox from '@/hooks/messageBox.js';

const formItems = ref([
  {
    name: '舆情词',
    key: 'opinionKey',
    type: 'input',
  },

  {
    name: '状态',
    key: 'isDisable',
    type: 'select',
    options: [
      { label: '启用', value: 0 },
      { label: '停用', value: 1 },
    ],
  },
  {
    name: '类型',
    key: 'level',
    type: 'select',
    options: [
      { label: '重要舆情词', value: 1 },
      { label: '一般舆情词', value: 2 },
    ],
  },
]);
const searchParams = ref({
  opinionKey: '',
  isDisable: '',
  typeId: '',
  level: '',
});
const baseTable = ref(null);
const sortTablekey = ref(1);
const handleSearch = (data = { sort: undefined }, e = '') => {
  if (e != 'sort') sortTablekey.value = 0;
  baseTable.value.handleSearch({
    ...searchParams.value,
    updateTimeOrder: data?.sort,
  });
};
const item = ref({});
const visibleEditForm = ref(false);
const handleEdit = (row) => {
  item.value = { ...row };
  visibleEditForm.value = true;
};
const handleDel = (row) => {
  openMsgBox(
    {},
    () => {
      return contentManagementService.libOpinionKeyDeleteBatch({
        data: [row.opinionKeyId],
      });
    },
    () => {
      refreshLeft();
      handleSearch();
    },
  );
};
const handleDisable = (row, rows = []) => {
  let isDisable = row.isDisable || row;
  let ids = row.opinionKeyId ? [row.opinionKeyId] : rows.map((e) => e.opinionKeyId);

  openMsgBox(
    {
      tips: `是否确定${isDisable == 1 ? '启用' : '停用'}?`,
    },
    () => {
      if (isDisable == 1) {
        return contentManagementService.libOpinionKeyEnableBatch({
          data: ids,
        });
      } else {
        return contentManagementService.libOpinionKeyDisableBatch({
          data: ids,
        });
      }
    },
    () => {
      refreshLeft();
      handleSearch();
    },
  );
};
const cateList = ref(null);
const handleEditForm = () => {
  refreshLeft();
  handleSearch();
};
const refreshLeft = () => {
  cateList.value.handleSearch();
};
</script>

<style lang="less" scoped></style>
